package com.yequan.file.zip;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * @author yq
 * @date 2020/06/29
 */
public class UploadFilesUtils {

    /**
     * 解压压缩包文件
     *
     * <dependency>
     * <groupId>org.apache.commons</groupId>
     * <artifactId>commons-compress</artifactId>
     * <version>1.19</version>
     * </dependency>
     *
     * @param fromPathStr
     * @param targetPathStr
     * @throws IOException
     */
    public static void decompression(String fromPathStr, String targetPathStr) {
        long startTime = System.currentTimeMillis();
        ZipArchiveInputStream inputStream = null;
        ZipArchiveEntry entry = null;
        try {
            inputStream = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(fromPathStr)));
            Files.createDirectories(Paths.get(targetPathStr));
            while ((entry = inputStream.getNextZipEntry()) != null) {
                if (entry.isDirectory()) {
                    Files.createDirectories(Paths.get(targetPathStr, entry.getName()));
                } else {
                    OutputStream outputStream = null;
                    try {
                        outputStream = new BufferedOutputStream(new FileOutputStream(new File(targetPathStr, entry.getName())));
                        //输出文件路径信息
                        IOUtils.copy(inputStream, outputStream);
                    } finally {
                        if (null != outputStream) {
                            outputStream.close();
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != inputStream) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long consumeTime = System.currentTimeMillis() - startTime;
        System.out.println("nio解压缩文件花费时间:" + consumeTime);
    }

    /**
     * 解压缩
     *
     * @param zipFilepath zip文件路径
     * @param dirFile     解压保存目录file
     */
    public static void unzip(String zipFilepath, File dirFile) {
        try {
            long startTime = System.currentTimeMillis();
            if (!new File(zipFilepath).exists()) {
                throw new RuntimeException("zip file " + zipFilepath + " does not exist.");
            }
            Project proj = new Project();
            Expand expand = new Expand();
            expand.setProject(proj);
            expand.setTaskType("unzip");
            expand.setTaskName("unzip");
            expand.setEncoding("GBK");

            expand.setSrc(new File(zipFilepath));
            expand.setDest(dirFile);
            expand.execute();
            long consumeTime = System.currentTimeMillis() - startTime;
            System.out.println("ant压缩文件花费时间:" + consumeTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        String path = "E:\\TEMP\\4.zip";
        String nioTargetPath = "E:\\TEMP\\nio";
        String antTargetPath = "E:\\TEMP\\ant";
        Thread.sleep(30000);
        decompression(path, nioTargetPath);
//        unzip(path, new File(antTargetPath));
        Thread.sleep(50000);
    }

}
